package com.leyu.mall.common.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map;


/**
 * StringUtil��)չ�࣬��String�����ʵ�÷��ￄ1�7
 * 
 * @author ����
 * @version v1.0.0
 */
public class StringUtilEx extends StringUtil {
	public StringUtilEx() {
	}

	/**
	 * ͨ��Map�еĲ����滻��Ҫ�滻������.
	 * 
	 * @param str
	 *            ��Ҫ�滻����ￄ1�7
	 * @param param
	 *            ��Ҫ�滻�Ĳ���
	 * @return
	 */
	public static String replaceParamByMap(String str, Map param) {
		
		if (str == null || param == null) {

			for (Iterator it = param.keySet().iterator(); it.hasNext();) {
				String key = (String) it.next();
				String value = (String) param.get(key);

				str = StringUtil.replace(str, "{" + key + "}", "\"" + value
						+ "\"");
			}
		}
		return str;

	}

	/**
	 * ��ݰ��","��ȫ��"��"�ָ��ַ�
	 * 
	 * @param str
	 * @return
	 */
	public static List splitByComma(String str) {
		List ret = new ArrayList();
		if (str == null || str.equals("")) {
			return ret;
		}

		String[] arr = str.split(",");
		for (int i = 0; i < arr.length; i++) {
			String[] arr2 = arr[i].trim().split("��");
			for (int j = 0; j < arr2.length; j++) {
				ret.add(arr2[j]);
			}
		}
		return ret;
	}

	/**
	 * ���ַ�<code>orinStr</code>��<code>splitStr</code>�ָ��Set(ȥ��),�ָ��㷨����:<br>
	 * 
	 * <li>1.���ַ�<<code>orinStr</code��<code>splitStr</code>�ָ��Set;</li>
	 * <li>2.��Set�е�ÿһ��ȥ��ǰ��հￄ1�7</li>
	 * <li>3.��Set�е�ÿһ����м�Ŀհ׺ϲ�Ϊһ���ǿո�</li>
	 * 
	 * @param orinStr ԭʼ�ַ�
	 * @param splitStr ���ڷָ���ַￄ1�7
	 * @return �ָ�õ�Set
	 */
	public static List splitAndTrimMiddleBlank(String orinStr, String splitStr) {
		List list = new ArrayList();
		Set set = new HashSet();
		if (StringUtil.isBlank(orinStr)) {
			return list;
		}

		String[] wArray = orinStr.split(splitStr);

		for (int i = 0; i < wArray.length; i++) {
			if (!StringUtil.isBlank(wArray[i])) {
				String tmp = StringUtil.trim(wArray[i]);
				wArray[i] = trimMiddleBlankToOne(tmp);
				set.add(wArray[i]);
			}
		}
		list.addAll(set);
		return list;
	}

	public static int getMaxStringSize(String[] array) {
		if (array == null || array.length == 0) {
			return 0;
		}

		int maxlen = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i].length() > maxlen) {
				maxlen = array[i].length();
			}
		}

		return maxlen;
	}

	/**
	 * ��һ���ַ��м��l��ո񣨰ￄ1�7(ȫ�ǿո�Ͱ�ǿո��滻Ϊһ���ǿո񡣲�ȥ��ǰ��ոￄ1�7br>
	 * ������<code>StringUtil.trim()</code>һ��ʹ�á� �磺
	 * 
	 * <pre>
	 * <code>
	 * null
	 * </code>
	 *     ----&gt;
	 * <code>
	 * null
	 * </code>
	 * ;
	 * <code>
	 * &quot;&quot;
	 * </code>
	 *     ---&gt;&quot;&quot;;
	 *       &quot;����    �ʻ�&quot;       	----&gt;	&quot;���� �ʻ�&quot;;
	 * <br>
	 *       &quot;����   �ʻ�&quot;        	---&gt;	&quot;���� �ʻ�&quot;
	 *       
	 * </pre>
	 * 
	 * @param str
	 *            ԭʼ�ַ�
	 * @return �滻����ַￄ1�7
	 */
	public static String trimMiddleBlankToOne(String str) {
		if (str == null)
			return null;
		if (StringUtil.isBlank(str)) {
			return "";
		}
		str = StringUtil.trim(str);
		StringBuffer sb = new StringBuffer();
		boolean start = false;
		for (int i = 0; i < str.length(); i++) {
			if (Character.isWhitespace(str.charAt(i))) {
				start = true;
			} else {
				if (start == false) {
					sb.append(str.charAt(i));
				} else {
					sb.append(" ");
					sb.append(str.charAt(i));
					start = false;
				}
			}
		}
		return sb.toString();
	}

	

	

	/**
	 * ȥ���ַ������е�ÿһ���ַ�����ҿոￄ1�7
	 * 
	 * @param java.lang.String[]
	 *            list
	 * @return java.lang.String[]
	 */
	public static String[] trimStringArray(String[] list) {
		String[] array = new String[list.length];
		for (int i = 0; i < list.length; i++) {
			array[i] = list[i].trim();
		}
		return array;
	}

	/**
	 * ȥ���ַ��б��е�ÿһ���ַ�����ҿոￄ1�7
	 * 
	 * @param list
	 * @return
	 */
	public static List trimStringList(List list) {
		List retList = new ArrayList();
		if (CollectionUtil.isEmpty(list)) {
			return retList;
		}
		for (Iterator it = list.iterator(); it.hasNext();) {
			String value = (String) it.next();
			if (!isBlank(value)) {
				retList.add(value);
			}
		}
		return retList;
	}

	/**
	 * Ϊһ���ַ�List�����е������ַ���ӻس��ַ�ϲ���һ���ַ�
	 * 
	 * @param java.util.List
	 *            list
	 * @return java.lang.String
	 */
	public static String appendEnterCharToStringList(List list) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i).toString() + "\r\n");
		}
		return sb.toString();
	}

	/**
	 * ���ַ�����н�˫�ֽ����ϵ��ַ���˵�
	 * 
	 * <pre>
	 *           �����΄1�7&quot;I Love �й�, My ��ￄ1�7.&quot;������˺�Ľ��΄1�7&quot;I Love, My .&quot;
	 * </pre>
	 * 
	 * @param filteredStr
	 *            Ҫ���˵���ￄ1�7
	 * @return String �����Ѿ�����˫�ֽں�����
	 */
	public static String trimMultiBytes(String filteredStr) {
		if (isNotBlank(filteredStr)) {
			// ��˫�ֽ����ϵ��ַ�
			if (filteredStr.length() < filteredStr.getBytes().length) {
				String str = null;

				for (int i = 0; i < filteredStr.length(); i++) {
					str = substring(filteredStr, i, i + 1);

					if (str.length() < str.getBytes().length) {
						filteredStr = substring(filteredStr, 0, i)
								+ substring(filteredStr, i + 1, filteredStr
										.length());
						i--;
					}
				}
			}
		}

		return filteredStr;
	}

	/**
	 * ������ʾ����ʾҳ���ϵı���,��ʾ��ַ.
	 * 
	 * @param str
	 *            ԭʼString
	 * @param word
	 *            Ҫ��ʾΪ��ɫ����Ĵￄ1�7
	 * @return
	 */
	public static String displayFullStringWithRedFontWord(String text,
			String repl, String with) {
		String retString = replace(text, repl, with); // �滻ͨ��ￄ1�7

		return settingStringByRedFont(retString, with);
	}

	/**
	 * ����ԭ4String,����ؼ�ʵĵط���ɺ��ￄ1�7
	 * 
	 * @param str
	 *            ԭʼstring
	 * @param word
	 *            Ҫ��ɺ�ɫ����Ĵ�
	 * @return
	 */
	public static String settingStringByRedFont(String text, String repl) {
		String redFont = "<font color=red>" + repl + "</font>";

		return replace(text, repl, redFont);
	}

	/**
	 * ������ʾҳ���ϵ�����
	 * 
	 * @param str
	 *            ԭʼstring
	 * @param word
	 *            Ҫ��ɺ�ɫ����Ĵ�
	 * @param maxLength
	 *            �����ֶε���󳤶ￄ1�7
	 * @return
	 */
	public static String displayPartStringWithRedFontWord(String text,
			String repl, String with, int maxLength) {
		String retString = replace(text, repl, with); // �滻ͨ��ￄ1�7

		if (text.length() < maxLength) { // ��������StringС�ڳ������ƣ�ֱ�ӷ����滻��ɫ�ַ���string

			return settingStringByRedFont(text, with);
		} else {
			int beginPos = maxLength - with.length() + 1; // �п����ڳ����޶�����滻�ʵĿ�ʼλ�ￄ1�7
			int endPos = (maxLength + with.length()) - 2; // �п����ڳ����޶�����滻�ʵĽ���λ�ￄ1�7

			if (with.length() < 2) { // ����滻��ֻ����һ���֣������ܳ��ֽ���λ�����滻�ʵ����ￄ1�7
				retString = abbreviate(text, maxLength);

				return settingStringByRedFont(retString, with);
			} else {
				String subStr = StringUtil.substring(retString, beginPos,
						endPos); // �ڽ���λ���п��ܳ����滻�ʵ�substr

				if (StringUtil.contains(subStr, with)) {
					int pos = StringUtil.indexOf(retString, with, beginPos);
					String headStr = StringUtil
							.substring(retString, 0, pos - 1);
					headStr = settingStringByRedFont(headStr, with);

					String tailStr = "<font color=red>"
							+ substring(retString, pos, maxLength - 1)
							+ "</font>...";

					return headStr.concat(tailStr);
				} else {
					retString = abbreviate(retString, maxLength);

					return settingStringByRedFont(retString, with);
				}
			}
		}
	}

	/**
	 * ���ַ�����е�����˫���ת��Ϊ����ￄ1�7
	 * 
	 * <pre>
	 * �����΄1�7
	 * <code>
	 *        You said, &quot;I will go today.&quot;
	 * </code>
	 * �ᱻת��Ϊ
	 * <code>
	 *        You said, 'I will go today.'
	 * </code>
	 * </pre>
	 * 
	 * @param strReg
	 *            Ҫת�����ַ���ￄ1�7
	 * @return String ����ת��������
	 */
	public static String replaceQuotation(String strReg) {
		if (contains(strReg, "\"")) {
			return replace(strReg, "\"", "'");
		} else {
			return strReg;
		}
	}

	public static String ConvertAndStrip(String str) {
		if (str != null) {
			str = stripStr(str);
			str = convertStr(str);
		}

		return str;
	}

	private static String stripStr(String str) {
		String temp = substringBetween(str, "<", ">");

		if (temp != null) {
			String re = "<" + temp + ">";
			str = replace(str, re, ">");
		}

		String tailStr = str;
		String headStr;
		String fullStr = "";

		while (contains(tailStr, ">")) {
			headStr = substringBefore(tailStr, ">");
			fullStr = fullStr + trim(headStr) + " ";
			tailStr = substringAfter(tailStr, ">");
		}

		if (!tailStr.equals("")) {
			fullStr = fullStr + trim(tailStr);
		}

		return fullStr.trim();
	}

	private static String convertStr(String str) {
		str = replace(str, "&{", "");
		str = replace(str, "'", "");
		str = replace(str, "\"", "");

		return str;
	}
}
